package com.example.oss.controller;

import com.example.commonutils.ResultData;
import com.example.oss.entity.UrlObject;
import com.example.oss.service.OssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/eduoss")
public class OssController {
    @Autowired
    private OssService service;

    @PostMapping(value = "/fileoss",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData uploadOssFile(MultipartFile file){
        //方法返回访问路径
        String url = service.uploadFileAvatar(file);
        return ResultData.ok().data("url",url);
    }
    //根据文件在阿里云中的路径删除图片
    @DeleteMapping(value = "deletePhoto/{url}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData deletePhoto(@PathVariable("url") String url){
        String fileName=url.substring(url.indexOf("/",10));
        fileName=fileName.substring(1);
        System.out.println(fileName);
        service.deletePhoto(fileName);
        return ResultData.ok();
    }

    //根据文件在阿里云中的路径删除图片
    @PostMapping(value = "deletePhoto",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData deletePhoto(@RequestBody UrlObject url){
        System.out.println(url);
        String url1 = url.getUrl();
        String fileName=url1.substring(url1.indexOf("/",10));
        fileName=fileName.substring(1);
        System.out.println(fileName);
        service.deletePhoto(fileName);
        return ResultData.ok();
    }
}
